﻿package com.dLibs.Slider
{
	
	import flash.display.*;
	import flash.events.*;
	
	import com.greensock.*;
	
	public class slideClip extends MovieClip
	{
		
		private var _contClip:MovieClip;
		private var _maskClip:MovieClip;
		private var _nextClip:MovieClip;
		private var _prevClip:MovieClip;
		private var _orientation:String;
		
		private var _maxWidth:Number;
		private var _maxHeight:Number;
		private var _actWidth:Number;
		private var _actHeight:Number;
		
		public function slideClip(contClip:MovieClip, maskClip:MovieClip, nextClip:MovieClip, prevClip:MovieClip, orientation:String = "horizontal")
		{			
			_contClip = contClip;
			_maskClip = maskClip;
			_nextClip = nextClip;
			_prevClip = prevClip;
			_orientation = orientation;
			
			_nextClip.buttonMode = true;
			_prevClip.buttonMode = true;
			
			_maxWidth = _maskClip.width;
			_actWidth = _maskClip.x;
			
			if ( _orientation == "vertical" )
			{
				_maxHeight = _maskClip.height;
				_actHeight = _maskClip.y;
			}
			
			_contClip.mask = _maskClip;
			
			slideInit();
		}
		
		public function verifyControl():void
		{
			if ( _orientation == "vertical" )
			{
				if ( _contClip.height <= _maxHeight ) { _nextClip.visible = _prevClip.visible = false; }
				else 								{ _nextClip.visible = _prevClip.visible = true; }
			}
			else
			{
				if ( _contClip.width <= _maxWidth ) { _nextClip.visible = _prevClip.visible = false; }
				else 								{ _nextClip.visible = _prevClip.visible = true; }
			}
		}
		
		private function slideInit():void
		{
			_nextClip.addEventListener(MouseEvent.CLICK, showNext);
			_prevClip.addEventListener(MouseEvent.CLICK, showPrev);
			
			verifyControl();
		}
		
		private function showNext(e:MouseEvent):void
		{
			if ( _orientation == "vertical" )
			{
				if ( int(_actHeight) > int((_maskClip.y + _maxHeight ) - _contClip.height)  )
				{
					TweenLite.to(_contClip,.9, {y : _actHeight - _maskClip.height});
					_actHeight = _actHeight - _maskClip.height;
				}
			}
			else
			{
				if ( int(_actWidth) > int((_maskClip.x + _maxWidth ) - _contClip.width)  )
				{
					TweenLite.to(_contClip,.9, {x : _actWidth - _maskClip.width});
					_actWidth = _actWidth - _maskClip.width;
				}
			}
		}
		
		private function showPrev(e:MouseEvent):void
		{
			if ( _orientation == "vertical" )
			{
				if ( int(_actHeight) < int(_maskClip.y) )
				{ 
					TweenLite.to(_contClip,.9, {y : _actHeight + _maskClip.height});
					_actHeight = _actHeight + _maskClip.height;
				}
			}
			else
			{
				if ( int(_actWidth) < int(_maskClip.x) )
				{ 
					TweenLite.to(_contClip,.9, {x : _actWidth + _maskClip.width});
					_actWidth = _actWidth + _maskClip.width;
				}
			}
		}
		
		public function reset():void
		{
			_contClip.x = _maskClip.x;
		}
	}
}